<?php require_once('../Connections/locale.php'); ?><?php require_once('../Connections/locale.php'); 
require("../includes/class.phpmailer.php");



$colname_testo_email = "-1";
if (isset($_POST['id_email'])) {
  $colname_testo_email = (get_magic_quotes_gpc()) ? $_POST['id_email'] : addslashes($_POST['id_email']);
}
mysql_select_db($database_locale, $locale);
$query_testo_email = sprintf("SELECT * FROM email_predefinite WHERE id_email = %s", $colname_testo_email);
$testo_email = mysql_query($query_testo_email, $locale) or die(mysql_error());
$row_testo_email = mysql_fetch_assoc($testo_email);
$totalRows_testo_email = mysql_num_rows($testo_email);

$query_ElencoEmails = $_POST['query'];
$ElencoEmails = mysql_query($query_ElencoEmails, $locale) or die(mysql_error());
$row_ElencoEmails = mysql_fetch_assoc($ElencoEmails);
$totalRows_ElencoEmails = mysql_num_rows($ElencoEmails);
?>

<?php
mysql_select_db($database_locale, $locale);
$query_opzioni_mail = "SELECT * FROM siteoptions";
$opzioni_mail = mysql_query($query_opzioni_mail, $locale) or die(mysql_error());
$row_opzioni_mail = mysql_fetch_assoc($opzioni_mail);
$totalRows_opzioni_mail = mysql_num_rows($opzioni_mail);

$mail = new PHPMailer();
$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = $row_opzioni_mail['smtp_server'];  // specify main and backup server
$mail->From = $row_opzioni_mail['sender_address'];
$mail->FromName = "Abbonamenti"

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  
  <?php do { ?>
  
  
  Invio Email a <?php echo $row_ElencoEmails['email']; ?> 
  <?php 
  $messaggio_temp = $row_testo_email['messaggio']; 
  $messaggio_temp = str_replace('#NOMINATIVO#',$row_ElencoEmails['nominativo'], $messaggio_temp);
  $messaggio_temp = str_replace('#RIVISTA#',$row_ElencoEmails['descrizione'], $messaggio_temp);
  $messaggio_temp = str_replace('#SCADENZA#',$row_ElencoEmails['data_scadenza'], $messaggio_temp);
  
  
  //Invio Email
  $mail->AddAddress($row_ElencoEmails['email'], $row_ElencoEmails['nominativo']);

//$mail->AddReplyTo("info@example.com", "Information");
//$mail->WordWrap = 50;                                 // set word wrap to 50 characters


$mail->IsHTML(true);                                  // set email format to HTML
$mail->Subject =$row_testo_email['subject'];
$mail->Body    = $messaggio_temp;
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";



if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo " Messaggio inviato!";
$mail->ClearAllRecipients();
  
  //echo $messaggio_temp;
  

  
  
  
  
  ?>
  <br />
  
    <?php } while ($row_ElencoEmails = mysql_fetch_assoc($ElencoEmails)); ?>




</body>
</html>
<?php
mysql_free_result($testo_email);

mysql_free_result($opzioni_mail);

mysql_free_result($ElencoEmails);
?>
